/*
 * array_queue.h
 *
 *  Created on: Mar 8, 2012
 *      Author: lin
 */

#ifndef C_ARRAY_QUEUE_H_
#define C_ARRAY_QUEUE_H_

#include "def/defs.h"
#include "concurrent.h"

typedef void (*c_array_queue_ele_free_proc)(void* ele);

typedef struct c_array_queue {
    uint32_t max_size;
    uint32_t size_mask;
    uint32_t size;spin spin;
    uint32_t eq_index;
    uint32_t dq_index;

    c_array_queue_ele_free_proc ele_del;

    complex_ele_t array[0];
} c_array_queue_t;

c_array_queue_t* c_array_queue_new0(uint32_t max_size,
        c_array_queue_ele_free_proc ele_del);
c_array_queue_t* c_array_queue_new1(uint32_t max_size);

void c_array_queue_free(c_array_queue_t* queue);

int c_array_queue_eq(c_array_queue_t* queue, const void* ele);
int c_array_queue_eq_s64(c_array_queue_t* queue, int64_t ele);
int c_array_queue_eq_u64(c_array_queue_t*queue, uint64_t ele);

void* c_array_queue_dq(c_array_queue_t* queue);
int c_array_queue_dq_s64(c_array_queue_t* queue, int64_t *s64);
int c_array_queue_dq_u64(c_array_queue_t* queue, uint64_t *u64);

static inline int c_array_queue_is_empty(c_array_queue_t* queue) {
    return atomic_get(&queue->size) == 0;
}

static inline int c_array_queue_is_full(c_array_queue_t* queue) {
    return atomic_get(&queue->size) == queue->max_size;
}

static inline uint32_t c_array_queue_size(c_array_queue_t* queue) {
    return atomic_get(&queue->size);
}

#endif /* ARRAY_QUEUE_H_ */
